import Vue from "vue";
import VueRouter from "vue-router";

//主页
const Home = () => import("@/pages/Home");
//import Home from "@/pages/Home"
//登录
const Logins = () => import("@/pages/Logins");
const Scan = () => import("@/pages/Logins/pages/Scan");
const Account = () => import("@/pages/Logins/pages/Account");
/* import Logins from "@/pages/Logins"
import Scan from "@/pages/Logins/pages/Scan"
import Account from "@/pages/Logins/pages/Account" */

//注册
const Register = () => import("@/pages/Register");
//import Register from "@/pages/Register"

//搜索
const Search = () => import("@/pages/Search");
//import Search from "@/pages/Search"

//详情页
const Detail = () => import("@/pages/Detail");
//import Detail from "@/pages/Detail"

//添加购物车成功页
const AddCaartSuccess = () => import("@/pages/AddCartSuccess");
//import AddCaartSuccess from "@/pages/AddCartSuccess"

//购物车页面
const ShopCart = () => import("@/pages/ShopCart");
//import ShopCart from "@/pages/ShopCart"


//订单页
const Trade = () => import("@/pages/Trade");
//import Trade from "@/pages/Trade"

//支付页
const Pay = () => import("@/pages/Pay");
//import Pay from "@/pages/Pay"

//支付成功页
const PaySuccess = () => import("@/pages/PaySuccess");
//import PaySuccess from "@/pages/PaySuccess"

const Center = () => import("@/pages/Center");
//import Center from "@/pages/Center"

const Myoder = () => import("@/pages/Center/Myoder");
//import Myoder from "@/pages/Center/Myoder"

const Alloder = () => import("@/pages/Center/Alloder");
//import Alloder from "@/pages/Center/Alloder"


const oldpush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return oldpush.call(this, location).catch(err => err)
}





import store from "@/store"
Vue.use(VueRouter)
const router = new VueRouter({
    routes: [{
        //主页
        name: "home",
        path: "/home",
        component: Home,
    }, {
        path: "/",
        redirect: "/home",
    }, {
        //登录
        name: "login",
        path: "/login",
        component: Logins,
        redirect: "/login/account",
        meta: {
            isShow: true
        },
        children: [{
                name: "scan",
                path: "scan",
                component: Scan,
                meta: {
                    isShow: true
                }
            },
            {
                name: "account",
                path: "account",
                component: Account,
                meta: {
                    isShow: true
                }
            }
        ]
    }, {
        //注册
        name: "register",
        path: "/register",
        component: Register,
        meta: {
            isShow: true
        }
    }, {
        //搜索
        path: "/Search/:val?",
        name: "Search",
        component: Search,
        props(route) {
            return {
                ...route.query
            }
        }
    }, {
        path: "/detail/:skuId?",
        name: "detail",
        component: Detail
    }, {
        path: "/addcartsuccess/:skuNum?",
        name: "Success",
        component: AddCaartSuccess,
        beforeEnter: (to, from, next) => {
            //判断是否携带购物车数据skuNum
            let skuNum = to.params.skuNum;
            let skuinfo = sessionStorage.getItem("skuInfo_key");
            if (skuNum && skuinfo) {
                next();
            } else {
                next(false)
            }


        }
    }, {
        path: "/ShopCart",
        name: "ShopCart",
        component: ShopCart
    }, {
        path: "/Trade",
        name: "Trade",
        component: Trade,
        beforeEnter: (to, from, next) => {
            if (from.path === "/ShopCart") {
                next();
            } else {
                next(false)
            }


        }

    }, {
        path: "/Pay",
        name: "Pay",
        component: Pay,
        beforeEnter: (to, from, next) => {
            if (from.path === "/Trade") {
                next();
            } else {
                next(false)
            }


        }
    }, {
        path: "/PaySuccess",
        name: "PaySuccess",
        component: PaySuccess,
        beforeEnter: (to, from, next) => {
            if (from.path === "/pay") {
                next();
            } else {
                next(false)
            }


        }
    }, {
        path: "/Center",
        component: Center,
        children: [{
            path: "Myoder",
            name: "Myoder",
            component: Myoder
        }, {
            path: "Alloder",
            name: "Alloder",
            component: Alloder
        }, {
            path: "",
            redirect: "/Center/Myoder"
        }]
    },{
        path: '/communication',
        component: () => import('@/pages/Communication/Communication'),
        children: [
          {
            path: 'event',
            component: () => import('@/pages/Communication/EventTest/EventTest'),
            meta: {
              isHideFooter: true
            },
          },
          {
            path: 'model',
            component: () => import('@/pages/Communication/ModelTest/ModelTest'),
            meta: {
              isHideFooter: true
            },
          },
          {
            path: 'sync',
            component: () => import('@/pages/Communication/SyncTest/SyncTest'),
            meta: {
              isHideFooter: true
            },
          },
          {
            path: 'attrs-listeners',
            component: () => import('@/pages/Communication/AttrsListenersTest/AttrsListenersTest'),
            meta: {
              isHideFooter: true
            },
          },
          {
            path: 'children-parent',
            component: () => import('@/pages/Communication/ChildrenParentTest/ChildrenParentTest'),
            meta: {
              isHideFooter: true
            },
          },
          {
            path: 'scope-slot',
            component: () => import('@/pages/Communication/ScopeSlotTest/ScopeSlotTest'),
            meta: {
              isHideFooter: true
            },
          }
        ],
      }],
    scrollBehavior() {
        return {
            x: 0,
            y: 0
        }
    }
})

router.beforeEach(async (to, from, next) => {
    const token = store.state.User.token;
    const userInfo = store.state.User.userInfo
    if (token) {
        if (to.path === "/login/scan" || to.path === "/login/account") {
            next("/")
        } else {
            if (userInfo) {
                next()
            } else {
                try {
                    await store.dispatch("UserInfo")
                    next();
                } catch (e) {
                    alert("token过期")
                    store.dispatch("clear")
                    next("/login")
                }
            }
        }
    } else {
        console.log(to.path)
        if (to.path === "/pay" || to.path === "/Trade" || to.path.includes("/Center")) {

            console.log(to.path)
            return next("/login?to=" + to.path)

        }
        next()
    }
})

export default router